package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test4 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        File dir=new File(Test4.class.getResource(".").toURI());
        File[] subs=dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub:subs){
            String className=sub.getName().substring(0,sub.getName().indexOf("."));
            Class cls=Class.forName(Test4.class.getPackage().getName()+"."+className);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println(cls.getName()+"正在实例化");
                Object o = cls.newInstance();
                Method[] methods=cls.getDeclaredMethods();
                for (Method m:methods){
                    if (m.isAnnotationPresent(AutoRunMethod.class)){
                        m.setAccessible(true);
                        m.invoke(o);
                    }
                }
            }

        }
    }
}
